<?php

namespace app\shop\controller;

use think\Controller;

class Cart extends Controller
{
  protected $BusinessModel;
  protected $CartModel;
  protected $ProductModel;

  public function __construct()
  {
    parent::__construct();

    $this->BusinessModel = model('Business.Business');
    $this->CartModel = model('Product.Cart');
    $this->ProductModel = model('Product.Product');
  }

  public function index()
  {
    if ($this->request->isGet()) {
      $busid = $this->request->param('busid', '0', 'trim');
      $ids = $this->request->param('ids', '0', 'trim');
      
      $business = $this->BusinessModel->where('id', $busid)->find();

      if (empty($business)) {
        $this->error('该用户不存在');
        exit;
      }

      $where = [
        'busid' => $busid,
      ];

      if(!empty($ids)){
        $where['cart.id'] = ['in', $ids];
      }

      $cart = $this->CartModel->with(['product'])->where($where)->select();
      // var_dump($this->CartModel->getLastSql());exit;
      if ($cart) {
        $this->success('ok', null, $cart);
        exit;
      } else {
        $this->error('购物车为空');
        exit;
      }
    }
  }

  // 加入购物车
  public function add()
  {
    if ($this->request->isPost()) {
      $busid = $this->request->param('busid', '0', 'trim');
      $proid = $this->request->param('proid', '0', 'trim');
      $business = null;
      $product = null;

      // 判断用户是否存在
      if ($busid) {
        $business = $this->BusinessModel->where('id', $busid)->find();

        if (empty($business)) {
          $this->error('用户不存在');
          exit;
        }
      } else {
        $this->error('用户不存在');
        exit;
      }

      // 判断用户是否存在
      if ($proid) {
        $where = ['status' => '1'];
        $product = $this->ProductModel->where('id', $proid)->where($where)->find();

        if (empty($product)) {
          $this->error('商品不存在');
          exit;
        }

        if ($product['stock'] <= 0) {
          $this->error('商品售罄');
          exit;
        }
        
      } else {
        $this->error('商品不存在');
        exit;
      }

      // 查询商品是否在购物车中
      $where = [
        'busid' => $busid,
        'proid' => $proid
      ];

      $check = $this->CartModel->where($where)->find();

      if (empty($check)) {
        $data = [
          'busid' => $busid,
          'proid' => $proid,
          'nums' => 1,
          'price' => $product['price'],
          'total' => $product['price']
        ];

        $res = $this->CartModel->validate('common/Product/Cart')->save($data);

        if ($res === false) {
          $this->error($this->CartModel->getError());
          exit;
        } else {
          $this->success('添加成功');
          exit;
        }
      } else {
        $nums =  bcadd($check['nums'], 1);
        $price = $product['price'];
        $total = bcmul($price, $nums);
        $data = [
          'id' => $check['id'],
          'nums' => $nums,
          'price' => $price,
          'total' => $total
        ];

        $res = $this->CartModel->validate('common/Product/Cart.edit')->isUpdate(true)->save($data);

        if ($res === false) {
          $this->error($this->CartModel->getError());
          exit;
        } else {
          $this->success('更新购物车数量成功');
          exit;
        }
      }
    }
  }

  // 修改购物数数量
  public function edit()
  {
    if ($this->request->isPost()) {
      $id = $this->request->param('id', '0', 'trim');
      $busid = $this->request->param('busid', '0', 'trim');
      $proid = $this->request->param('proid', '0', 'trim');
      $nums = $this->request->param('nums', 0, 'trim');
      $business = null;
      $product = null;

      // 判断用户是否存在
      if ($busid) {
        $business = $this->BusinessModel->where('id', $busid)->find();

        if (empty($business)) {
          $this->error('用户不存在');
          exit;
        }
      } else {
        $this->error('用户不存在');
        exit;
      }

      // 判断用户是否存在
      if ($proid) {
        $where = ['status' => '1'];
        $product = $this->ProductModel->where('id', $proid)->where($where)->find();

        if (empty($product)) {
          $this->error('商品不存在');
          exit;
        }

        if ($product['stock'] <= 0) {
          $this->error('商品售罄');
          exit;
        }
      } else {
        $this->error('商品不存在');
        exit;
      }

      // 查询商品是否在购物车中
      $check = $this->CartModel->where('id', $id)->find();

      if (empty($check)) {
        $this->error('购物车记录不存在');
        exit;
      }

      if ($nums <= 0) {
        $this->error('购物车数量不能为0');
        exit;
      }

      // 库存不足时如果是减少就不提醒
      if ($product['stock'] < $nums && $nums > $check['nums']) {
        $this->error('库存不足');
        exit;
      }

      $price = $product['price'];
      $total = bcmul($price, $nums, 2);
      $data = [
        'id' => $check['id'],
        'nums' => $nums,
        'price' => $price,
        'total' => $total
      ];

      $res = $this->CartModel->validate('common/Product/Cart.edit')->isUpdate(true)->save($data);

      if ($res === false) {
        $this->error($this->CartModel->getError());
        exit;
      } else {
        $this->success('更新购物车数量成功');
        exit;
      }
    }
  }

  // 删除购物车记录
  public function del()
  {
    if($this->request->isPost()){
      $id = $this->request->param('id', '0', 'trim');
      $busid = $this->request->param('busid', '0', 'trim');

      // 判断用户是否存在
      if ($busid) {
        $business = $this->BusinessModel->where('id', $busid)->find();

        if (empty($business)) {
          $this->error('用户不存在');
          exit;
        }
      } else {
        $this->error('用户不存在');
        exit;
      }

      // 判断购物车记录是否存在
      if($id){
        $check = $this->CartModel->where('id', $id)->find();

        if(empty($check)){
          $this->error('购物车记录不存在');
          exit;
        }
      }

      $res = $this->CartModel->where('id', $id)->delete();

      if($res === false){
        $this->error('删除失败');
        exit;
      }else{
        $this->success('删除成功');
        exit;
      }
    }
  }
}
